package me.work.pay.jsyl.app.utils;

import android.content.Context;
import android.media.MediaPlayer;
import android.util.Log;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

import me.work.pay.jsyl.R;

public class SoundMaker {
    private boolean isPlay;
    private Queue<String> soundqueue;

    MediaPlayer[] sound;
    int now_position = 0;
    List<MediaPlayer> playerList = new ArrayList<>();
    Context mContext;

    public SoundMaker(final Context context) {
        isPlay = false;
        mContext = context;
        sound = new MediaPlayer[27];
        soundqueue = new LinkedList<String>();
        sound[0] = MediaPlayer.create(context, R.raw.a0);
        sound[1] = MediaPlayer.create(context, R.raw.a1);
        sound[2] = MediaPlayer.create(context, R.raw.a2);
        sound[3] = MediaPlayer.create(context, R.raw.a3);
        sound[4] = MediaPlayer.create(context, R.raw.a4);
        sound[5] = MediaPlayer.create(context, R.raw.a5);
        sound[6] = MediaPlayer.create(context, R.raw.a6);
        sound[7] = MediaPlayer.create(context, R.raw.a7);
        sound[8] = MediaPlayer.create(context, R.raw.a8);
        sound[9] = MediaPlayer.create(context, R.raw.a9);
        sound[10] = MediaPlayer.create(context, R.raw.shi);
        sound[11] = MediaPlayer.create(context, R.raw.bai);
        sound[12] = MediaPlayer.create(context, R.raw.qian);
        sound[13] = MediaPlayer.create(context, R.raw.dian);//点
        sound[14] = MediaPlayer.create(context, R.raw.wx);//微信
        sound[15] = MediaPlayer.create(context, R.raw.zfb);//支付宝
        sound[16] = MediaPlayer.create(context, R.raw.yuan);//元
        sound[17] = MediaPlayer.create(context, R.raw.wan);//万
        sound[18] = MediaPlayer.create(context, R.raw.a0);//shengji检测到新版本 设备升级中
        sound[19] = MediaPlayer.create(context, R.raw.qing_fukuan);//请扫码付款
//        sound[20] = MediaPlayer.create(context, R.raw.sj_success);//升级成功
//        sound[21] = MediaPlayer.create(context, R.raw.sj_fail);//升级失败
//
//        sound[22] = MediaPlayer.create(context, R.raw.pw1);//进入配网模式
//        sound[23] = MediaPlayer.create(context, R.raw.pw2);//配网模式已退出
//        sound[24] = MediaPlayer.create(context, R.raw.pw3);//无线网络连接成功
//        sound[25] = MediaPlayer.create(context, R.raw.pw4);//无线网络连接失败
//        sound[26] = MediaPlayer.create(context, R.raw.pw5);//正在连接无线网络

//        for (int i = 0; i < 14; i++) {
//            sound[i].setOnCompletionListener(mediaPlayer -> {
//                isPlay = false;
//                if (!soundqueue.isEmpty()) {
//                    sound[Integer.parseInt(soundqueue.poll())].start();
//                    isPlay = true;
//                }
//            });
//        }
    }

    public void makeSound(String type, String num) {
        isPlay = false;
        int sound_id = -1;
        soundqueue = new LinkedList<String>();
        if (type.equals("3")) {
            try {
                sound_id = Integer.parseInt(num);
            } catch (Exception e) {
                switch (num) {
                    case ".":
                        sound_id = 13;
                        break;
                    case "@"://请扫码付款
                        sound_id = 19;
                        break;
                    case "联网成功":
                        break;

                }
            }
            soundqueue.add(String.valueOf(sound_id));
        } else {
            switch (type) {
                case "1":
                    sound_id = 14;
                    soundqueue.add("14");
                    break;
                case "2":
                    sound_id = 15;
                    soundqueue.add("15");
                    break;
            }
            String end_money = "";//最终的钱
            String left_money = MoneyUtils.readInt(Integer.parseInt(num.split("\\.")[0]));
            String right_money = num.split("\\.")[1];
            end_money = left_money + "." + right_money;
            char[] c = end_money.toCharArray();
            for (int i = 0; i < c.length; i++) {
                switch (String.valueOf(c[i])) {
                    case "十":
                        soundqueue.add("10");
                        break;
                    case "百":
                        soundqueue.add("11");
                        break;
                    case "千":
                        soundqueue.add("12");
                        break;
                    case "万":
                        soundqueue.add("17");
                        break;
                    case ".":
                        soundqueue.add("13");
                        break;
                    default:
                        soundqueue.add(String.valueOf(c[i]));
                        break;
                }
            }
//            soundqueue.poll();//去掉第一个声音
            soundqueue.add("16");
        }
        Log.e("sea:??", sound_id + "--sound_id" + num);

//        if (!isPlay) {
//            Log.e("sea:??", sound_id + "--1" + num);
//            if (sound_id != -1) {
//                sound[sound_id].start();
//                isPlay = true;
//            }
//        } else {
//            Log.e("sea:??", sound_id + "--2" + num);
//            if (sound_id != -1) {
//                soundqueue.offer("0");
//            }
//        }
        play_sound();
    }

    int play_position = -1;

    private void play_sound() {
        play_position = -1;
        playerList = new ArrayList<>();
        sound = new MediaPlayer[soundqueue.size()];
        int sound_id = 0;
        int now_index = 0;
        for (String s : soundqueue) {
            switch (s) {
                case "0":
                    sound_id = R.raw.a0;
                    break;
                case "1":
                    sound_id = R.raw.a1;
                    break;
                case "2":
                    sound_id = R.raw.a2;
                    break;
                case "3":
                    sound_id = R.raw.a3;
                    break;
                case "4":
                    sound_id = R.raw.a4;
                    break;
                case "5":
                    sound_id = R.raw.a5;
                    break;
                case "6":
                    sound_id = R.raw.a6;
                    break;
                case "7":
                    sound_id = R.raw.a7;
                    break;
                case "8":
                    sound_id = R.raw.a8;
                    break;
                case "9":
                    sound_id = R.raw.a9;
                    break;
                case "10":
                    sound_id = R.raw.shi;
                    break;
                case "11":
                    sound_id = R.raw.bai;
                    break;
                case "12":
                    sound_id = R.raw.qian;
                    break;
                case "13":
                    sound_id = R.raw.dian;
                    break;
                case "14":
                    sound_id = R.raw.wx;
                    break;
                case "15":
                    sound_id = R.raw.zfb;
                    break;
                case "16":
                    sound_id = R.raw.yuan;
                    break;
                case "17":
                    sound_id = R.raw.wan;
                    break;
                case "18":
                    sound_id = R.raw.a0;
                    break;
                case "19":
                    sound_id = R.raw.qing_fukuan;
                    break;
                case "20":
                    sound_id = R.raw.sj_success;
                    break;
                case "21":
                    sound_id = R.raw.sj_fail;
                    break;
                case "22":
                    sound_id = R.raw.pw1;
                    break;
                case "23":
                    sound_id = R.raw.pw2;
                    break;
                case "24":
                    sound_id = R.raw.pw3;
                    break;
                case "25":
                    sound_id = R.raw.pw4;
                    break;
                case "26":
                    sound_id = R.raw.pw5;
                    break;
                default:
                    sound_id = -1;
                    break;
            }
            //预加载声音文件
            if (sound_id != -1) {
                sound[now_index] = MediaPlayer.create(mContext, sound_id);
                int finalNow_index = now_index;
                if (now_index == 0) {
                    sound[0].start();
                }
                sound[now_index].setOnCompletionListener(mediaPlayer -> {
                    isPlay = false;
                    if (sound.length > finalNow_index + 1) {
                        play_position = finalNow_index + 1;//当前播放的位置
                        sound[play_position].start();
                        isPlay = true;
                    }
                });
                now_index++;
            }
        }
    }
}